﻿package com.tudou.privacy
{
    import com.tudou.events.*;
    import com.tudou.model.*;
    import com.tudou.net.*;
    import com.tudou.skin.*;
    import com.tudou.track.*;
    import flash.display.*;
    import flash.events.*;
    import flash.ui.*;

    public class PrivacyPanel extends Sprite
    {
        private var panel:PrivacyPanelMC;
        private var iBaseInfo:IBaseInfo;
        private var title:String;
        public static const INIT:int = 0;
        public static const SUCCESS:int = 2;
        public static const SUBMITTING:int = 1;
        public static const FAIL:int = -1;

        public function PrivacyPanel(param1:IBaseInfo, param2:String)
        {
            this.iBaseInfo = param1;
            this.title = param2;
            panel = new PrivacyPanelMC();
            addChild(panel);
            panel.passwordInputText.border = true;
            panel.passwordInputText.backgroundColor = 15658734;
            panel.passwordInputText.background = true;
            panel.passwordInputText.text = "";
            panel.passwordInputText.addEventListener(FocusEvent.FOCUS_IN, focusHlr);
            panel.passwordInputText.addEventListener(KeyboardEvent.KEY_UP, keyUpHlr);
            panel.ok.addEventListener(MouseEvent.CLICK, clickOKHlr);
            panel.contactUploadUser.addEventListener(MouseEvent.CLICK, contactHlr);
            panel.searchRelativeVideos.addEventListener(MouseEvent.CLICK, searchHlr);
            status = INIT;
            return;
        }// end function

        private function searchHlr(event:MouseEvent) : void
        {
            openSearch(title);
            PrivacyTracker.send(iBaseInfo, PrivacyTracker.SEARCH);
            return;
        }// end function

        private function clickOKHlr(event:MouseEvent) : void
        {
            if (password)
            {
                sumbitHlr();
            }
            return;
        }// end function

        public function set status(param1:int) : void
        {
            switch(param1)
            {
                case INIT:
                {
                    panel.ok.visible = true;
                    panel.submitting.visible = false;
                    panel.wrongPasswordTip.visible = false;
                    break;
                }
                case SUBMITTING:
                {
                    panel.ok.visible = false;
                    panel.submitting.visible = true;
                    panel.wrongPasswordTip.visible = false;
                    break;
                }
                case SUCCESS:
                {
                    this.visible = false;
                    PrivacyTracker.send(iBaseInfo, PrivacyTracker.SUCCEED);
                    break;
                }
                case FAIL:
                {
                    panel.ok.visible = true;
                    panel.submitting.visible = false;
                    panel.wrongPasswordTip.visible = true;
                    break;
                }
                default:
                {
                    this.visible = false;
                    break;
                }
            }
            return;
        }// end function

        private function sumbitHlr() : void
        {
            dispatchEvent(new PrivacyPanelEvent(PrivacyPanelEvent.SUBMIT, password));
            PrivacyTracker.send(iBaseInfo, PrivacyTracker.SUBMIT);
            return;
        }// end function

        private function contactHlr(event:MouseEvent) : void
        {
            openContactUser(iBaseInfo.iid);
            PrivacyTracker.send(iBaseInfo, PrivacyTracker.CONTACT_USER);
            return;
        }// end function

        private function keyUpHlr(event:KeyboardEvent) : void
        {
            if (event.keyCode == Keyboard.ENTER && password)
            {
                sumbitHlr();
            }
            return;
        }// end function

        private function focusHlr(event:FocusEvent) : void
        {
            var fe:* = event;
            try
            {
                if (stage.displayState == StageDisplayState.FULL_SCREEN)
                {
                    stage.displayState = StageDisplayState.NORMAL;
                }
            }
            catch (e:Error)
            {
                trace(e);
            }
            return;
        }// end function

        public function get password() : String
        {
            return panel.passwordInputText.text;
        }// end function

    }
}
